﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.HR
{
    public partial class HR_MsalyList : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrp();
                ShowNoData(Grid1);
                BindGrid();
            }
        }
        protected void BindDrp()
        {
            for (int i = 1; i < 13; i++)
            {
                Drp_Mouth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
            }
            Drp_Mouth.SelectedValue = DateTime.Now.Month.ToString();
            Drp_Year.Items.Add(new ListItem((DateTime.Now.Year - 1).ToString() + "年", (DateTime.Now.Year - 1).ToString()));
            Drp_Year.Items.Add(new ListItem((DateTime.Now.Year).ToString() + "年", (DateTime.Now.Year).ToString()));
            Drp_Year.SelectedValue = DateTime.Now.Year.ToString();
        }

        protected void BindGrid()
        {
            WhereClip wc = WhereClip.All;
            if (ttbxMyBox2.Text != "")
            {
                wc = wc && (VI_EmpMSALY._.EMPLID.Contain(ttbxMyBox2.Text) || VI_EmpMSALY._.TrueName.Contain(ttbxMyBox2.Text));
            }
            int year = int.Parse(Drp_Year.SelectedValue);
            int mouth = int.Parse(Drp_Mouth.SelectedValue);
            int count = TbsDbSession.Default.From<VI_EmpMSALY>().Where(VI_EmpMSALY._.YEAR == year && VI_EmpMSALY._.MONTH == mouth && wc).Count();
            List<VI_EmpMSALY> msList = TbsDbSession.Default.From<VI_EmpMSALY>().Where(VI_EmpMSALY._.YEAR == year && VI_EmpMSALY._.MONTH == mouth && wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).OrderBy(VI_EmpMSALY._.EMPLID.Asc).ToList();
            Grid1.RecordCount = count;
            Grid1.DataSource = msList;
            Grid1.DataBind();
            CacheHelper.InsertIdentify("TempData", msList, 120, 0);
        }



        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid();
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid();
        }




        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string empno = e.Values[0].ToString();
            string year = Drp_Year.SelectedValue;
            string mouth = Drp_Mouth.SelectedValue;
            double a = double.Parse(e.Values[14].ToString());
            double b = 0;
            HR_Insurance hi = TbsDbSession.Default.From<HR_Insurance>().Where(HR_Insurance._.EmpNo == empno && HR_Insurance._.PayYear == year && HR_Insurance._.PayMonth == mouth).ToFirst();

            e.Values[11] = "<font color='red'>-" + e.Values[11] + "</font>";
            e.Values[12] = "<font color='red'>-" + e.Values[12] + "</font>";
            //e.Values[13] = "<font color='red'>-" + e.Values[13] + "</font>";
            if (hi != null)
            {
                b = double.Parse(hi.PersonAmount.Value.ToString());
                //e.Values[13] ="<font color='red'>-"+ hi.PersonAmount.Value.ToString()+"</font>";
                e.Values[13] = "<a href=\"javascript:;\" onclick=\"" + Window1.GetShowReference("HR_InsuranceChange.aspx?id=" + hi.ID) + "\">-" + hi.PersonAmount.Value.ToString() + "</a>";
            }
            else
            {
                //e.Values[13] = "<a href=\"javascript:;\" onclick=\"" + Window1.GetShowReference("HR_InsuranceChange.aspx?empid=" + empno) + "\">添加缴纳明细</a>";
                e.Values[13] = "<a href=\"javascript:;\" onclick=\"" + Window1.GetShowReference("HR_InsuranceChange.aspx?empid=" + empno) + "\">添加缴纳明细</a>";
            }
            a = a - b;
            decimal p = decimal.Parse(a.ToString());
           //p = decimal.Round(p);
            e.Values[14] = "<span style=\"color:red;font-weight:bold;\">" + p.ToString() + "</span>";

        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void btnMoney_Click(object sender, EventArgs e)
        {
            WhereClip wc = WhereClip.All;
            if (ttbxMyBox2.Text != "")
            {
                wc = wc && (VI_EmpMSALY._.EMPLID.Contain(ttbxMyBox2.Text) || VI_EmpMSALY._.TrueName.Contain(ttbxMyBox2.Text));
            }
            int year = int.Parse(Drp_Year.SelectedValue);
            int mouth = int.Parse(Drp_Mouth.SelectedValue);
            List<VI_EmpMSALY> msList = TbsDbSession.Default.From<VI_EmpMSALY>().Where(VI_EmpMSALY._.YEAR == year && VI_EmpMSALY._.MONTH == mouth && wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).OrderBy(VI_EmpMSALY._.EMPLID.Asc).ToList();
            CacheHelper.InsertIdentify("TempData", msList, 120, 0);
            PageContext.RegisterStartupScript(Window3.GetShowReference("Print_MSALY.aspx"));
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Print")
            {
                List<VI_EmpMSALY> msList = TbsDbSession.Default.From<VI_EmpMSALY>().Where(VI_EmpMSALY._.ID == id).ToList();
                CacheHelper.InsertIdentify("TempData", msList, 120, 0);
                PageContext.RegisterStartupScript("window.open('Print_MSALY.aspx');");
                //PageContext.RegisterStartupScript(Window3.GetShowReference("Print_MSALY.aspx"));
            }
        }
    }
}